package com.dalanmao.demo_20170606_numberview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by zhaoliang on 2017/6/9.
 */

public class NumberView extends View {

    //到达区域
    private int reachedBarColor;

    private float reachedBarHeight;

    private RectF reachedRectF;

    private Paint reachedPaint;


    //未到达区域
    private int unReachedBarColor;

    private float unReachedBarHeight;

    private RectF unReachedRectF;

    private Paint unReachedPaint;


    //文本
    private float textWidth;

    private float textStart;

    private float textEnd;

    private float textSize;

    private int textColor;

    private float textoffset;

    private boolean textVisiable;

    private Paint textPaint;

    //
    private int currentProgress;
    private int maxProgress;


    private String text;


    public NumberView(Context context) {
        this(context, null);
    }

    public NumberView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    /**
     * 初始化
     */
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {


        final TypedArray attr = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NumberView, defStyleAttr, 0);
        maxProgress = attr.getInt(R.styleable.NumberView_progress_max, 100);
        currentProgress = attr.getInt(R.styleable.NumberView_progress_current, 0);

        reachedBarColor = attr.getColor(R.styleable.NumberView_progress_reached_bar_color, Color.rgb(66, 145, 241));
        unReachedBarColor = attr.getColor(R.styleable.NumberView_progress_unreached_bar_color, Color.rgb(166, 145, 141));
        textColor = attr.getColor(R.styleable.NumberView_progress_text_color, Color.rgb(204, 204, 204));

        reachedBarHeight = attr.getDimension(R.styleable.NumberView_progress_reached_bar_height, dp2px(2.0f));
        unReachedBarHeight = attr.getDimension(R.styleable.NumberView_progress_unreached_bar_height, dp2px(2.0f));

        textSize = attr.getDimension(R.styleable.NumberView_progress_text_size, sp2px(10.0f));
        textoffset = attr.getDimension(R.styleable.NumberView_progress_text_offset, dp2px(2.0f));
        textVisiable = attr.getBoolean(R.styleable.NumberView_progress_text_visiable, true);

        attr.recycle();


        reachedRectF = new RectF();
        unReachedRectF = new RectF();

        reachedPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        reachedPaint.setColor(reachedBarColor);

        unReachedPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        unReachedPaint.setColor(unReachedBarColor);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);


    }


    /**
     * 确定绘制范围
     */
    private void calculateRectf() {
        unReachedRectF.left = getPaddingLeft();
        unReachedRectF.top = getHeight() / 2.0f - unReachedBarHeight / 2.0f;
        unReachedRectF.right = getWidth() * (currentProgress / (maxProgress * 1.0f)) + getPaddingLeft();
        unReachedRectF.bottom = getHeight() / 2.0f + unReachedBarHeight / 2.0f;


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //1.确定个区域的绘制范围

        calculateRectf();

        canvas.drawRect(unReachedRectF, unReachedPaint);

        text = currentProgress + "%";

        canvas.drawText(text, unReachedRectF.right + textoffset, getHeight() / 2.0f - (textPaint.descent() + textPaint.ascent()) / 2.0f, textPaint);


        Log.i("zl", "leading = " + textPaint.getFontMetrics().leading);
        Log.i("zl", "ascent  = " + textPaint.getFontMetrics().ascent);
        Log.i("zl", "descent = " + textPaint.getFontMetrics().descent);
        Log.i("zl", "top     = " + textPaint.getFontMetrics().top);
        Log.i("zl", "bottom  = " + textPaint.getFontMetrics().bottom);


        //2.绘制















    }


    public float dp2px(float dp) {
        final float scale = getResources().getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    public float sp2px(float sp) {
        final float scale = getResources().getDisplayMetrics().scaledDensity;
        return sp * scale;
    }

}
